Conditions | 1 |
Paths | 1 |
Total Lines | 107 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | /** |
||
16 | function (state, data, visibility, util, format, reactionService) { |
||
17 | let ct = this; |
||
18 | ct.state = state; |
||
19 | ct.data = data; |
||
20 | ct.util = util; |
||
21 | ct.format = format; |
||
22 | ct.adjustAmount = [1, 10, 25, 100]; |
||
23 | |||
24 | function update(player) { |
||
25 | for(let slot of player.element_slots){ |
||
26 | if(!slot){ |
||
27 | continue; |
||
28 | } |
||
29 | for (let reaction of slot.reactions) { |
||
30 | if (!reaction.active) { |
||
31 | continue; |
||
32 | } |
||
33 | reactionService.react(numberToReact(player, reaction.reaction), reaction.reaction, player); |
||
34 | } |
||
35 | } |
||
36 | } |
||
37 | |||
38 | function numberToReact(player, reaction) { |
||
39 | let power = util.calculateValue(data.global_upgrades.reaction_bandwidth.power.base, |
||
40 | data.global_upgrades.reaction_bandwidth.power, |
||
41 | player.global_upgrades_current.reaction_bandwidth); |
||
42 | let number = power; |
||
43 | for(let resource in reaction.reactants){ |
||
44 | number = Math.min(number, player.resources[resource].number); |
||
45 | } |
||
46 | return number; |
||
47 | } |
||
48 | |||
49 | ct.reactionSize = function (player) { |
||
50 | let size = 0; |
||
51 | for(let slot of player.element_slots){ |
||
52 | if(!slot){ |
||
53 | continue; |
||
54 | } |
||
55 | size += slot.reactions.length; |
||
56 | } |
||
57 | return size; |
||
58 | }; |
||
59 | |||
60 | /* Adds a new reaction to the player list */ |
||
61 | ct.addReaction = function (player, slot, key) { |
||
62 | if(ct.reactionSize(player) >= util.calculateValue(data.global_upgrades.reaction_slots.power.base, |
||
63 | data.global_upgrades.reaction_slots.power, |
||
64 | player.global_upgrades.reaction_slots)){ |
||
65 | return; |
||
66 | } |
||
67 | let reaction = data.reactions[key]; |
||
68 | slot.reactions.push({ |
||
69 | active: false, |
||
70 | reaction: angular.copy(reaction) |
||
71 | }); |
||
72 | }; |
||
73 | |||
74 | ct.removeReaction = function (slot, item) { |
||
75 | for(let i = 0; i < slot.reactions.length; i++){ |
||
76 | if(slot.reactions[i] === item){ |
||
77 | slot.reactions.splice(i, 1); |
||
78 | } |
||
79 | } |
||
80 | }; |
||
81 | |||
82 | ct.visibleReactions = function(slot) { |
||
83 | return slot.reactions; |
||
84 | }; |
||
85 | |||
86 | ct.availableReactions = function(slot) { |
||
87 | return visibility.visible(data.reactions, isReactionAvailable, slot.element); |
||
88 | }; |
||
89 | |||
90 | function isReactionAvailable(entry, currentElement) { |
||
91 | let available = true; |
||
92 | let reaction = data.reactions[entry]; |
||
93 | for(let resource in reaction.reactant){ |
||
94 | available = available && state.player.resources[resource].unlocked; |
||
95 | } |
||
96 | // Workaround to reuse the visibility function. It expects an object with the |
||
97 | // reaction inside |
||
98 | let reactionObject = {reaction:reaction}; |
||
99 | return available && isReactionVisible(reactionObject, currentElement); |
||
100 | } |
||
101 | |||
102 | function isReactionVisible(entry, currentElement) { |
||
103 | let reaction = entry.reaction; |
||
104 | if(reaction.elements.length === 0){ |
||
105 | return true; |
||
106 | } |
||
107 | for(let element of reaction.elements){ |
||
108 | if(element === currentElement){ |
||
109 | return true; |
||
110 | } |
||
111 | } |
||
112 | return false; |
||
113 | } |
||
114 | |||
115 | ct.adjustLevel = function(player, upgrade, amount){ |
||
116 | player.global_upgrades_current[upgrade] += amount; |
||
117 | // We cap it between 1 and the current max level |
||
118 | player.global_upgrades_current[upgrade] = Math.max(1, Math.min(player.global_upgrades_current[upgrade], player.global_upgrades[upgrade])); |
||
119 | }; |
||
120 | |||
121 | state.registerUpdate('reactions', update); |
||
122 | }]); |
||
123 |